package com.sinosoft.sinopay.web.gateway.biz.service.impl;

import com.sinosoft.sinocloud.platform.base.SinoBaseService;
import com.sinosoft.sinocloud.platform.common.exception.biz.BizDataErrException;
import com.sinosoft.sinopay.api.common.dto.pay.PayAccountInfoDTO;
import com.sinosoft.sinopay.api.common.dto.pay.PaymentInfoDetailDTO;
import com.sinosoft.sinopay.api.common.dto.query.PayInfoQryParam;
import com.sinosoft.sinopay.paytrade.config.ENUM_ACCOUNT_TYPE;
import com.sinosoft.sinopay.paytrade.config.ENUM_ACCOUNT_USETYPE;
import com.sinosoft.sinopay.paytrade.dto.PaymentConfirmDTO;
import com.sinosoft.sinopay.paytrade.dto.PaymentRequestDTO;
import com.sinosoft.sinopay.paytrade.dto.UserAuthConfInfo;
import com.sinosoft.sinopay.paytrade.facade.PaymentTradeFacadeService;
import com.sinosoft.sinopay.paytrade.facade.PaymentTradeQueryFacadeService;
import com.sinosoft.sinopay.web.gateway.biz.config.GatewayPaymentConstants;
import com.sinosoft.sinopay.web.gateway.biz.service.GatewaySecurityService;
import com.sinosoft.sinopay.web.gateway.biz.service.PaymentGatewayService;
import com.sinosoft.sinopay.web.gateway.dto.PayEntryRequestData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PaymentGatewayServiceImpl extends SinoBaseService implements PaymentGatewayService{
	@Autowired
	private PaymentTradeFacadeService paymentTradeFacadeService;
	@Autowired
	private PaymentTradeQueryFacadeService paymentTradeQryFacadeService;
	@Autowired
	private GatewaySecurityService gatewaySecurityService;
	
	@Override
	public String dealGatewayPayRequest(PayEntryRequestData cPayInfo) {
		String tBankCode = cPayInfo.getBankCode();
		String tBankName = cPayInfo.getBankName();
		String tAccountNo = cPayInfo.getAccountNo();
		String tAccountName = cPayInfo.getAccountName();
		String tIdType = cPayInfo.getIdType();
		String tIdNo = cPayInfo.getIdNo();
		String tMobile = cPayInfo.getMobile();
		//1.持久化支付信息
		PaymentRequestDTO tPayRequestDTO = new PaymentRequestDTO();
		tPayRequestDTO.setBizNo(cPayInfo.getBizNo());
		tPayRequestDTO.setBizType(cPayInfo.getBizType());
		tPayRequestDTO.setMerchantCode(cPayInfo.getMerchantCode());
		tPayRequestDTO.setPayAmount(cPayInfo.getPayAmount());
		tPayRequestDTO.setSerialNo(cPayInfo.getSerialNo());
		tPayRequestDTO.setRemark(cPayInfo.getTransDesc());
		//封装付款账户信息dto
		PayAccountInfoDTO tPayAccount = new PayAccountInfoDTO();
		tPayAccount.setAccountName(tAccountName);
		tPayAccount.setAccountNo(tAccountNo);
		tPayAccount.setAccountType(ENUM_ACCOUNT_TYPE.DEBIT.code());
		tPayAccount.setBankCode(tBankCode);
		tPayAccount.setBankName(tBankName);
		tPayAccount.setCardNo(tIdNo);
		tPayAccount.setCardType(tIdType);
		tPayAccount.setEasyPayFlag("N");
		tPayAccount.setMobile(tMobile);
		tPayAccount.setPayIndex(0);
		tPayAccount.setUseType(ENUM_ACCOUNT_USETYPE.TEMP.code());
		tPayRequestDTO.setPayAccount(tPayAccount);
		//调用交易服务接口，完成交易请求持久化操作
		String tPayNo = paymentTradeFacadeService.newPayTrade(tPayRequestDTO);
		logger.info("生成支付号:"+tPayNo);
		//2.生成合法支付页面url
		Long timestap = new Date().getTime();
		//签名算法待确定
		Map<String,String> tSignInfoMap = new HashMap<String,String>();
		tSignInfoMap.put("serialNo",cPayInfo.getSerialNo());//交易流水号
		tSignInfoMap.put("bizNo",cPayInfo.getBizNo());//业务号码
		tSignInfoMap.put("payAmount",cPayInfo.getPayAmount().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());//支付金额
		tSignInfoMap.put("merchantCode",cPayInfo.getMerchantCode());//商户编码
		tSignInfoMap.put("transDesc",cPayInfo.getTransDesc());//交易描述
		String signMsg = gatewaySecurityService.createSignMsg(cPayInfo.getMerchantCode(),tSignInfoMap);

		StringBuffer tPayPageUrl = new StringBuffer(GatewayPaymentConstants.PAY_PAGE_REQUEST_URL);
		tPayPageUrl.append("&payNo=");
		tPayPageUrl.append(tPayNo);
		tPayPageUrl.append("&orderNo=");
		tPayPageUrl.append(cPayInfo.getBizNo());
		tPayPageUrl.append("&signMsg=");
		tPayPageUrl.append(signMsg);
		tPayPageUrl.append("&timeStap=");
		tPayPageUrl.append(timestap);
		//3.返回支付URL
		return tPayPageUrl.toString();
	}

	@Override
	public PaymentInfoDetailDTO queryPaymentInfoByPayNo(String cPayNo) {
		PaymentInfoDetailDTO paymentInfoDetailDTO = paymentTradeQryFacadeService.queryPaymentOrderByPayNo(cPayNo);
		if(paymentInfoDetailDTO == null){
			throw new BizDataErrException("支付号不存在，payno为:" + cPayNo);
		}
		return paymentInfoDetailDTO;
	}

	@Override
	public UserAuthConfInfo confirmPayment(String cPayNo) {
		PaymentConfirmDTO paymentConfirmDTO = new PaymentConfirmDTO();
		paymentConfirmDTO.setPayNo(cPayNo);
		return paymentTradeFacadeService.comfirmTradeRequest(cPayNo);
	}

	@Override
	public void modifyTradeOrderStatus(PaymentConfirmDTO cPayConfirmInfo) {
		paymentTradeFacadeService.modifyTradeOrderStatus(cPayConfirmInfo);
	}
}
